
const mongoose = require('mongoose');
const config = require('./index')
const log4js = require('../utils/log4j')
const Event = require('../utils/event')

const Menu = require('../models/menuSchema')
const Depts = require('../models/deptSchema')
const Roles = require('../models/roleSchema')

const menuList = require('./menus.json')
const depts = require('./depts.json')
const roles = require('./roles.json')

mongoose.connect(config.URL, {
  useNewUrlParser: true, // <-- no longer necessary
  useUnifiedTopology: true // <-- no longer necessary
})

const db = mongoose.connection;

log4js.info('-------准备连接数据库-------')

db.on('error', () => {
  log4js.error('-------数据库连接失败-------')
})

db.on('open', () => {
  log4js.info('-------数据库连接成功-------')
  Event.emit('dbConnect')
  // for (const item of roles) {
  //   Roles.create(item)
  // }

  // for (const item of menuList) {
  //   Menu.create(item)
  // }

  // for (const item of depts) {
  //   Depts.create(item)
  // }

})

module.exports = db
